package View;

import DB.Usuario.ControllerUsuario;
import Model.Usuario;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class UsuarioDetailView extends javax.swing.JFrame {

    Usuario u;

    /**
     * Creates new form UsersView
     *
     * @param user
     */
    public UsuarioDetailView(Usuario user) {
        u = user;

        initComponents();

        textID.setText(String.valueOf(u.getId()));
        textName.setText(u.getNome());
        textUsername.setText(u.getUsername());
        textPassword.setText(u.getPassword());

        textOperador.setText(String.valueOf(u.getOperador()));

        textCPF.setText(u.getCpf());
        textEmail.setText(u.getEmail());
        textNascimento.setText(u.getDataNascimento());
        textBadgeName.setText(u.getNomeCracha());
        textSex.setText(u.getSexo());
        textAddress.setText(u.getEndereco());
        textCity.setText(u.getCidade());
        textState.setText(u.getEstado());
        textCountry.setText(u.getPais());
        textCEP.setText(u.getCep());
        textPhone.setText(u.getTelefone());
        textCellphone.setText(u.getCelular());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btAtualizar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        textID = new javax.swing.JTextField();
        textUsername = new javax.swing.JTextField();
        textPassword = new javax.swing.JTextField();
        textOperador = new javax.swing.JTextField();
        textName = new javax.swing.JTextField();
        textCPF = new javax.swing.JTextField();
        textNascimento = new javax.swing.JTextField();
        textBadgeName = new javax.swing.JTextField();
        textSex = new javax.swing.JTextField();
        labelID = new javax.swing.JLabel();
        labelUsername = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        labelOperador = new javax.swing.JLabel();
        labelName = new javax.swing.JLabel();
        labelCPF = new javax.swing.JLabel();
        labelNascimento = new javax.swing.JLabel();
        labelBadgeName = new javax.swing.JLabel();
        labelSex = new javax.swing.JLabel();
        labelAddress = new javax.swing.JLabel();
        labelCity = new javax.swing.JLabel();
        labelCountry = new javax.swing.JLabel();
        labelState = new javax.swing.JLabel();
        labelCEP = new javax.swing.JLabel();
        labelPhone = new javax.swing.JLabel();
        labelCellphone = new javax.swing.JLabel();
        textPhone = new javax.swing.JTextField();
        textCellphone = new javax.swing.JTextField();
        textCity = new javax.swing.JTextField();
        textAddress = new javax.swing.JTextField();
        textCountry = new javax.swing.JTextField();
        textState = new javax.swing.JTextField();
        textCEP = new javax.swing.JTextField();
        labelEmail = new javax.swing.JLabel();
        textEmail = new javax.swing.JTextField();
        btEventos = new javax.swing.JButton();
        btInscrever = new javax.swing.JButton();

        setTitle("CADASTRO DE USUARIOS");
        setResizable(false);

        btAtualizar.setText("Atualizar");
        btAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAtualizarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        textID.setEditable(false);

        textUsername.setEditable(false);

        textPassword.setEditable(false);

        labelID.setText("ID:");

        labelUsername.setText("Usuário*:");

        labelPassword.setText("Senha*:");

        labelOperador.setText("Operador*:");

        labelName.setText("Nome*:");

        labelCPF.setText("CPF*:");

        labelNascimento.setText("Data nascimento:");

        labelBadgeName.setText("Nome cracha:");

        labelSex.setText("Sexo:");

        labelAddress.setText("Endereço:");

        labelCity.setText("Cidade:");

        labelCountry.setText("País:");

        labelState.setText("Estado:");

        labelCEP.setText("CEP:");

        labelPhone.setText("Telefone:");

        labelCellphone.setText("Celular:");

        labelEmail.setText("Email:");

        btEventos.setText("Eventos...");
        btEventos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEventosActionPerformed(evt);
            }
        });

        btInscrever.setText("Inscrever...");
        btInscrever.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btInscreverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btEventos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btInscrever)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btAtualizar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelCity)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCity, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelCountry)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelState)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textState)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelCEP)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCEP, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelAddress)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textAddress))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelBadgeName)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textBadgeName, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelSex)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textSex, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addComponent(labelPhone)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelNascimento)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textNascimento))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelCellphone)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textCellphone))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelEmail)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textEmail))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textID, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelUsername)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(labelPassword)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelOperador)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textOperador, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelCPF)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCPF)))
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelID)
                    .addComponent(textID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPassword)
                    .addComponent(labelOperador)
                    .addComponent(textOperador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelUsername)
                    .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelName)
                    .addComponent(labelCPF)
                    .addComponent(textCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEmail)
                    .addComponent(textEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelBadgeName)
                    .addComponent(labelNascimento)
                    .addComponent(textNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textBadgeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSex)
                    .addComponent(textSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPhone)
                    .addComponent(textPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCellphone)
                    .addComponent(textCellphone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelAddress)
                    .addComponent(textAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCity)
                    .addComponent(textCity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCountry)
                    .addComponent(textCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCEP)
                    .addComponent(textCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelState)
                    .addComponent(textState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAtualizar)
                    .addComponent(btCancelar)
                    .addComponent(btEventos)
                    .addComponent(btInscrever))
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(598, 355));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAtualizarActionPerformed
        u.setNome(textName.getText());

        if (textOperador.getText().equals("1") || textOperador.getText().equals("0")) {
            u.setOperador(Integer.valueOf(textOperador.getText()));
        } else {
            JOptionPane.showMessageDialog(null, "Valor inválido para o campo Operador.", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }

        u.setCpf(textCPF.getText());
        u.setEmail(textEmail.getText());
        u.setDataNascimento(textNascimento.getText());
        u.setSexo(textSex.getText());
        u.setNomeCracha(textBadgeName.getText());
        u.setTelefone(textPhone.getText());
        u.setCelular(textCellphone.getText());
        u.setEndereco(textAddress.getText());
        u.setCidade(textCity.getText());
        u.setPais(textCountry.getText());
        u.setEstado(textState.getText());
        u.setCep(textCEP.getText());

        ControllerUsuario uc = ControllerUsuario.getInstance();
        long resultado = 0;

        try {
            resultado = uc.update(u);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

        if (resultado > 0) {
            JOptionPane.showMessageDialog(null, "O usuário foi atualizado.");
        }
        this.setVisible(false);
    }//GEN-LAST:event_btAtualizarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btEventosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEventosActionPerformed
        UsuarioEventos ue = new UsuarioEventos(u);
        ue.setVisible(true);
    }//GEN-LAST:event_btEventosActionPerformed

    private void btInscreverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btInscreverActionPerformed
        InscricaoAddNew an = new InscricaoAddNew();
        an.setVisible(true);
    }//GEN-LAST:event_btInscreverActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAtualizar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btEventos;
    private javax.swing.JButton btInscrever;
    private javax.swing.JLabel labelAddress;
    private javax.swing.JLabel labelBadgeName;
    private javax.swing.JLabel labelCEP;
    private javax.swing.JLabel labelCPF;
    private javax.swing.JLabel labelCellphone;
    private javax.swing.JLabel labelCity;
    private javax.swing.JLabel labelCountry;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelID;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelNascimento;
    private javax.swing.JLabel labelOperador;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JLabel labelPhone;
    private javax.swing.JLabel labelSex;
    private javax.swing.JLabel labelState;
    private javax.swing.JLabel labelUsername;
    private javax.swing.JTextField textAddress;
    private javax.swing.JTextField textBadgeName;
    private javax.swing.JTextField textCEP;
    private javax.swing.JTextField textCPF;
    private javax.swing.JTextField textCellphone;
    private javax.swing.JTextField textCity;
    private javax.swing.JTextField textCountry;
    private javax.swing.JTextField textEmail;
    private javax.swing.JTextField textID;
    private javax.swing.JTextField textName;
    private javax.swing.JTextField textNascimento;
    private javax.swing.JTextField textOperador;
    private javax.swing.JTextField textPassword;
    private javax.swing.JTextField textPhone;
    private javax.swing.JTextField textSex;
    private javax.swing.JTextField textState;
    private javax.swing.JTextField textUsername;
    // End of variables declaration//GEN-END:variables
}
